<template>
  <div>
    <div class="mui-scroll-wrapper mui-content chat-container" :class="{active: showChoose}" style="top: 45px">
      <div class="mui-scroll">
        <div class="chatitem">
          <div class="msg-content">
            <div v-if="!msgList || !Object.getOwnPropertyNames(msgList.list).length"></div>
            <div class="msg" v-else :class="{self: item.self}" v-for="item in msgList.list" :key="item.time">
              <div class="avatar">
                <!--<img width="100%" height="100%" :src="item.self?self.avatar: user.avatar" alt="">-->
                <img width="100%" height="100%"
                :src="item.self?(currentLoginUser.headicon || $imHeadIcon): chatHeadicon" alt="网咯原因，找不到头像">
              </div>
              <div class="bubble"
              v-if='item.type && item.type === "img"'
              :class='{active:item.type && item.type === "img"}'>
                <img :src="item.msg" class="bubbleimg" alt="">
              </div>
              <div class="bubble" v-else v-html="item.msg">
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div id="bottom-chat"  ref="journalUpward"></div>
  </div>
</template>

<script>
  import {mapState, mapGetters} from 'vuex'
  export default {
    props: {
      showChoose: {
        type: Boolean,
        default: false
      },
      chatHeadicon: {
        type: String,
        default: this.$imHeadIcon
      }
      },
    computed: {
      ...mapState(['currentLoginUser']),
      ...mapGetters({msgList: 'getCurrentUserChat'})
    },
    created() {
      this.$immui.init()
      console.log(this.msgList, 'oooooooooo')
    },
    mounted() {
      const self = this
      self.$immui.init({
        gestureConfig: {
          tap: true
        }
      })
      self.$immui('.chat-container').scroll({
        deceleration: 0.0005,
        indicators: false
      })
      self.$immui('.chat-container').scroll().refresh()
      self.$immui('.chat-container').scroll().scrollToBottom(10)
      let y = self.$immui('.chat-container').scroll().y
      self.$immui('.chat-container').scroll().scrollTo(0, y + window.innerHeight - 100, 100)
    },
    watch: {
      msgList(newval) {
        const self = this
        setTimeout(() => {
          self.$immui('.chat-container').scroll().refresh()
          self.$immui('.chat-container').scroll().scrollToBottom(10)
        }, 500)
      },
      showChoose(newval) {
        const self = this
        setTimeout(() => {
          self.$immui('.chat-container').scroll().refresh()
          self.$immui('.chat-container').scroll().scrollToBottom(10)
        }, 50)
      }
    }
  }
</script>

<style lang='scss'  scoped>
  .msg-content .msg {
    width: 100%;
    padding: 5px 10px;
    box-sizing: border-box;
    font-size: 0;
    .avatar{
      display: inline-block;
      width: 40px;
      height: 40px;
      border-radius: 50%;
      overflow: hidden;
      background: white;
    }
    .bubble{
      display: inline-block;
      vertical-align: top;
      margin-left: 5px;
      padding: 10px 10px 10px 5px;
      border-radius: 4px;
      line-height: 20px;
      max-width: 50%;
      word-wrap: break-word;
      font-size: 14px;
      background: #fff;
      &.active{
        padding: 0!important;
        background-color: #f0f0f0;
      }
    }
    &.self {
      text-align: right;
      .avatar{
        float: right;
        margin-left: 5px;
      }
      .bubble {
        background: #4dace7;
        color: white;
        padding: 10px 5px 10px 10px;
        &.active{
        padding: 0!important;
        background-color: #f0f0f0;
      }
      }
    }
  }
  /*滚动条样式*/
  .chat-container{
    bottom: 50px;
    &.active{
      bottom: 250px;
    }
  }
  .bubbleimg{
    max-width: 175px;
    // max-height: 100px;
    padding: 0;
  }
</style>
